package com.orange.score.model.match;

import android.content.Context;
import android.database.Cursor;
import android.util.Log;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

public class FollowMatchManager
{
  private static final String TAG = "FollowMatchManager";
  private static FollowMatchManager manager = null;
  FollowMatchDBAdapter followMatchDBAdapter;
  ArrayList<Match> followMatchList = new ArrayList();
  HashMap<String, Match> followMatchMap = new HashMap();

  private void deleteMatch(Match paramMatch)
  {
    this.followMatchMap.remove(paramMatch.matchId);
    this.followMatchDBAdapter.removeEntry(paramMatch.getMatchId());
  }

  private static FollowMatchManager getInstance()
  {
    return manager;
  }

  private void load()
  {
    Cursor localCursor = this.followMatchDBAdapter.getAllEntries();
    if (localCursor == null);
    while (true)
    {
      return;
      localCursor.moveToFirst();
      if (!(localCursor.isAfterLast()))
        break;
      localCursor.close();
    }
    Match localMatch = new Match();
    localMatch.fromCursor(localCursor);
    int i = localCursor.getColumnIndex("match_id");
    if (i != -1)
      localMatch.setMatchId(localCursor.getString(i));
    while (true)
    {
      while (true)
      {
        this.followMatchMap.put(localMatch.getMatchId(), localMatch);
        localCursor.moveToNext();
      }
      Log.e("FollowMatchManager", "load all follow matches, but cursor index = -1");
    }
  }

  public void close()
  {
    if (this.followMatchDBAdapter != null)
      this.followMatchDBAdapter.close();
  }

  public void createOrUpdateMatch(Match paramMatch)
  {
    Cursor localCursor = this.followMatchDBAdapter.findEntryByMatch(paramMatch);
    if ((localCursor == null) || (localCursor.getCount() == 0))
      this.followMatchDBAdapter.insertEntry(paramMatch);
    while (true)
    {
      this.followMatchMap.put(paramMatch.matchId, paramMatch);
      if (localCursor != null)
        localCursor.close();
      return;
      this.followMatchDBAdapter.updateEntry(paramMatch);
    }
  }

  public Match findMatchById(String paramString)
  {
    return ((Match)this.followMatchMap.get(paramString));
  }

  public void followMatch(Match paramMatch)
  {
    paramMatch.setIsFollow(true);
    createOrUpdateMatch(paramMatch);
  }

  public ArrayList<Match> getFollowMatchList()
  {
    load();
    ArrayList localArrayList = new ArrayList(this.followMatchMap.values());
    Collections.sort(localArrayList);
    return localArrayList;
  }

  public void init(Context paramContext)
  {
    this.followMatchDBAdapter = new FollowMatchDBAdapter(paramContext);
    this.followMatchDBAdapter.open();
    load();
  }

  public boolean isMatchFollowed(String paramString)
  {
    return this.followMatchMap.containsKey(paramString);
  }

  public void setFollowMatchDBAdapter(FollowMatchDBAdapter paramFollowMatchDBAdapter)
  {
    this.followMatchDBAdapter = paramFollowMatchDBAdapter;
  }

  public void unfollowMatch(Match paramMatch)
  {
    paramMatch.setIsFollow(false);
    deleteMatch(paramMatch);
  }

  public void updateMatch(Match paramMatch)
  {
    this.followMatchDBAdapter.updateEntry(paramMatch);
    this.followMatchMap.put(paramMatch.matchId, paramMatch);
  }
}